package com.jzg.jcptadmin.presenter;

import com.jzg.jcptadmin.base.BasePresenter;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.vo.AppraisersData;
import com.jzg.jcptadmin.data.vo.CityItem;
import com.jzg.jcptadmin.viewinterface.OrganizationInterface;

import java.util.Map;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * author: guochen
 * date: 2016/7/7 19:21
 * email:
 */
public class OrganizationPresenter extends BasePresenter<OrganizationInterface> {
    private DataManager dataManager;
    private OrganizationInterface mView;
    private Subscription mSubscription;

    public OrganizationPresenter(DataManager dataManager) {
        this.dataManager = dataManager;
    }

    @Override
    public void attachView(OrganizationInterface mvpView) {
        super.attachView(mvpView);
        this.mView = mvpView;
    }

    @Override
    public void detachView() {
        super.detachView();
        if (mSubscription != null) mSubscription.unsubscribe();
    }

    public void getCityData(Map<String, String> params) {
        mSubscription = dataManager.getCityData(params)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.newThread()).subscribe(new Observer<CityItem>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e != null && mView != null) {
                            mView.showError(e.getMessage());
                        }
                    }

                    @Override
                    public void onNext(CityItem cityItem) {

                        mView.cityDataSuccess(cityItem);


                    }
                });
    }

    public void getOrganizationData(Map<String, String> params) {
        mSubscription = dataManager.getOrganizationData(params)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.newThread()).subscribe(new Observer<AppraisersData>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e != null && mView != null) {
                            mView.showError(e.getMessage());
                        }
                    }

                    @Override
                    public void onNext(AppraisersData appraisersData) {

                        mView.organizationDataSuccess(appraisersData);


                    }
                });
    }

}
